const webpack = require('webpack');
const { getPublicPath, pathResolve, getAssetPath, getEnv } = require('./util')
const TerserPlugin = require('terser-webpack-plugin');
const { plugins } = require('../../postcss.config');

const defaultConfig = {
    devServer: {
        open: true,
        allowedHosts: 'all',
        client: {
            progress: true
        }
        // disableHostCheck: true
    },
    css: {
        loaderOptions: {
            less: {
                javascriptEnabled: true,
            },
            sass: {
                additionalData: `@import "@/assets/styles/theme/default.scss";`
            },
        }
    },
    publicPath: getPublicPath(),
    outputDir: 'output',
    productionSourceMap: false,
    lintOnSave: false,
};

const defaultConfigureWebpackConfig = {
    
    devtool: !['production', 'production_temp'].includes(process.env.APP_DEPLOY_ENV) ? 'source-map' : false,
    resolve: {
        alias: {
            '@': pathResolve('src'),
            '@config': pathResolve('config'),
            '@common': pathResolve('common'),
        },
    },
    module: {
        rules: [
            {
                test: /\.(png|jpe?g|gif|webp|ico)(\?.*)?$/,
                type: 'asset/resource',
                generator: {
                    filename: `${getAssetPath()}/img/[name].[hash:8][ext][query]`,
                    publicPath: getPublicPath()
                }
            },
            {
                test: /\.(ttf|eot|woff2?)(\?.*)?$/,
                type: 'asset/resource',
                generator: {
                    filename: `${getAssetPath()}/fonts/[name].[hash:8][ext][query]`,
                    publicPath: getPublicPath()
                }
            },
            {
                test: /\.(svg?)$/,
                type: 'asset/source',
                parser: {
                    dataUrlCondition: {
                        maxSize: 10 * 1024
                    }
                },
                generator: {
                    filename: `${getAssetPath()}/img/[name].[hash:8][ext][query]`,
                    publicPath: getPublicPath()
                }
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                APP_DEPLOY_ENV: JSON.stringify(process.env.APP_DEPLOY_ENV)
            }
        }),
    ],
    optimization: {
        minimize: getEnv() === 'production',
        minimizer: [
            new TerserPlugin({
                // parallel: true, //使用多进程并发运行以提高构建速度 Boolean|Number 默认值： true  
                terserOptions: {
                    compress: {
                        drop_console: true
                    },
                    // toplevel: true,
                    keep_classnames: true,
                    keep_fnames: true
                },
            })
        ]
    }
}


module.exports = {
    defaultConfig,
    defaultConfigureWebpackConfig,
}
